Inside Macintosh: QuickTime

Previous | Chapter Top | Chapter Contents | Next

Playing a Movie

The easiest way to play a movie is to use a movie controller component. See the previous section for more information about using movie controller components. If you want to create your own control for playing movies, you should observe the following guidelines:

Once you have loaded a movie, you can play the movie. Your application must perform the following tasks:

  1. Create a window for the movie to play in.
  2. Position the movie in the window.
  3. Start the movie.
  4. Play the movie until it is done.
  5. Dispose of the movie when it is done playing.

When you play a movie, the Movie Toolbox processes the movie's data in the context of the movie's time coordinate system. If the movie contains video data, the Movie Toolbox displays the resulting image in the display window you specify. If the movie contains audio data, the Movie Toolbox plays that sound track at the volume you set.

You must call the MoviesTask function (described on MoviesTask ) repeatedly until the movie is done playing. Each time you call the MoviesTask function, the Movie Toolbox processes the movie you are playing, updates the display as appropriate, and uses the Sound Manager to play the movie's sound. You can use the IsMovieDone function (described on IsMovieDone ) to determine when the movie is finished playing.

The code in Listing 4 shows the steps your application must follow in order to play a movie. This program retrieves a movie, sizes the window properly, plays the movie forward, and exits. This program uses the GetMovie function, shown in Listing 2 on Getting a movie from a file to retrieve a movie from a movie file. The movie controller component supplied by Apple also plays a movie. For more information, see the chapter "Movie Controller Components" in Inside Macintosh: QuickTime Components .

Listing 4 Playing a movie

#include <Types.h>
#include <Traps.h>
#include <Menus.h>
#include <Fonts.h>
#include <Packages.h>
#include <GestaltEqu.h>
#include "Movies.h"
#include "ImageCompression.h"
/* #include "QuickTimeComponents.h" */

#define doTheRightThing 5000

void main (void)
{
    WindowPtr       aWindow;
    Rect            windowRect;
    Rect            movieBox;
    Movie           aMovie;
    Boolean         done = false;
    OSErr           err;
    EventRecord theEvent;
    WindowPtr       whichWindow;
    short           part;

    InitGraf (&qd.thePort);
    InitFonts ();
    InitWindows ();
    InitMenus ();
    TEInit ();
    InitDialogs (nil);
    err = EnterMovies ();
    if (err) return;
    
    SetRect (&windowRect, 100, 100, 200, 200);
    aWindow = NewCWindow (nil, &windowRect, "\pMovie",
                                 false, noGrowDocProc, (WindowPtr)-1,
                                 true, 0);

    SetPort (aWindow);
    aMovie = GetMovie ();
    if (aMovie == nil) return;
    
    GetMovieBox (aMovie, &movieBox);
    OffsetRect (&movieBox, -movieBox.left, -movieBox.top);
    SetMovieBox (aMovie, &movieBox);
    
    SizeWindow (aWindow, movieBox.right, movieBox.bottom, true);
    ShowWindow (aWindow);
    SetMovieGWorld (aMovie, (CGrafPtr)aWindow, nil);
    
    StartMovie (aMovie);
    
    while ( !IsMovieDone(aMovie) && !done )
    {
        if (WaitNextEvent (everyEvent, &theEvent, 0, nil))
        {
            switch ( theEvent.what )
            {
                case updateEvt:
                    whichWindow = (WindowPtr)theEvent.message;
                    if (whichWindow == aWindow)
                    {
                        BeginUpdate (whichWindow);
                        UpdateMovie(aMovie);
                        SetPort (whichWindow);
                        EraseRect (&whichWindow->portRect);
                        EndUpdate (whichWindow);
                    }
                    break;

                case mouseDown:
                    part = FindWindow (theEvent.where,
                                                 &whichWindow);
                    if (whichWindow == aWindow)
                    {
                        switch (part)
                        {
                            case inGoAway:
                                done = TrackGoAway (whichWindow,
                                                             theEvent.where);
                                break;
                            case inDrag:
                                DragWindow (whichWindow,
                                                 theEvent.where,
                                                 &qd.screenBits.bounds);
                                break;
                        }
                    }
                    break;
            }
        }
        MoviesTask (aMovie, DoTheRightThing);
    }
    DisposeMovie (aMovie);
    DisposeWindow (aWindow);
}

© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next